package com.dlc.commonlibrary.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.dlc.commonlibrary.R;

/**
 * Created by lizhiyong on 2015/12/28.
 */
public class TitleBar extends RelativeLayout {

    public final TextView titleText;
    public final ImageButton leftImage;
    public final ImageButton rightImage;
    public final TextView leftText;
    public final TextView rightText;
    private final View bottomLine;

    private int mHeight;

    public static final int TITLE_TEXT = R.id.titleText;
    public static final int LEFT_IMAGE = R.id.leftImage;
    public static final int RIGHT_IMAGE = R.id.rightImage;
    public static final int LEFT_TEXT = R.id.leftText;
    public static final int RIGHT_TEXT = R.id.rightText;

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.view_title_bar, this, true);

        titleText = (TextView) view.findViewById(TITLE_TEXT);
        leftImage = (ImageButton) view.findViewById(LEFT_IMAGE);
        rightImage = (ImageButton) view.findViewById(RIGHT_IMAGE);
        leftText = (TextView) view.findViewById(LEFT_TEXT);
        rightText = (TextView) view.findViewById(RIGHT_TEXT);
        bottomLine = view.findViewById(R.id.bottomLine);

        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {

        // 设置高度
        mHeight = getResources().getDimensionPixelSize(R.dimen.title_bar_height);

        int backgroundColor = ContextCompat.getColor(getContext(), R.color.title_bar_background);
        int lineColor = ContextCompat.getColor(getContext(), R.color.title_bottom_line_color);
        int textColor = ContextCompat.getColor(getContext(), R.color.title_text_color);
        int buttonBg = R.drawable.title_bar_button_bg;
        int textBg = R.drawable.title_bar_text_bg;

        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBar);

            String titleText = typedArray.getString(R.styleable.TitleBar_titleText);

            this.titleText.setText(titleText);

            // 下方分隔线
            lineColor = typedArray.getColor(R.styleable.TitleBar_lineColor, lineColor);
            bottomLine.setBackgroundColor(lineColor);

            boolean showLine = typedArray.getBoolean(R.styleable.TitleBar_showLine, false);

            bottomLine.setVisibility(showLine ? VISIBLE : GONE);

            // 左侧按钮
            int leftDrawableRes = typedArray.getResourceId(R.styleable.TitleBar_leftDrawable, 0);

            if (leftDrawableRes != 0) {
                leftImage.setImageResource(leftDrawableRes);
            }

            boolean showLeftImage =
                typedArray.getBoolean(R.styleable.TitleBar_showLeftImage, leftDrawableRes != 0);

            leftImage.setVisibility(showLeftImage ? VISIBLE : GONE);

            // 右侧按钮
            int rightDrawableRes = typedArray.getResourceId(R.styleable.TitleBar_rightDrawable, 0);

            if (rightDrawableRes != 0) {
                rightImage.setImageResource(rightDrawableRes);
            }

            boolean showRightImage =
                typedArray.getBoolean(R.styleable.TitleBar_showRightImage, rightDrawableRes != 0);

            rightImage.setVisibility(showRightImage ? VISIBLE : GONE);

            // 左侧文字
            String leftString = typedArray.getString(R.styleable.TitleBar_leftText);
            leftText.setText(leftString);

            boolean showLeftText = typedArray.getBoolean(R.styleable.TitleBar_showLeftText,
                !TextUtils.isEmpty(leftString));
            leftText.setVisibility(showLeftText ? VISIBLE : GONE);

            // 右侧文字
            String rightString = typedArray.getString(R.styleable.TitleBar_rightText);
            rightText.setText(rightString);
            boolean showRightText = typedArray.getBoolean(R.styleable.TitleBar_showRightText,
                !TextUtils.isEmpty(rightString));
            rightText.setVisibility(showRightText ? VISIBLE : GONE);

            backgroundColor =
                typedArray.getColor(R.styleable.TitleBar_backgroundColor, backgroundColor);

            buttonBg = typedArray.getResourceId(R.styleable.TitleBar_imageBg,
                R.drawable.title_bar_button_bg);

            textBg =
                typedArray.getResourceId(R.styleable.TitleBar_textBg, R.drawable.title_bar_text_bg);

            typedArray.recycle();
        }
        setBackgroundColor(backgroundColor);
        setImageBackground(buttonBg);
        setTextBackground(textBg);
        setTitleTextColor(textColor);
    }

    public void setImageBackground(int resId) {
        leftImage.setBackgroundResource(resId);
        rightImage.setBackgroundResource(resId);
    }
    public void setRightImageBackground(int resId) {
        rightImage.setImageResource(resId);
        rightImage.setVisibility(VISIBLE );
    }

    public void setTextBackground(int resId) {
        leftText.setBackgroundResource(resId);
        rightText.setBackgroundResource(resId);
    }

    public void setTitleTextColor(int color) {
        titleText.setTextColor(color);
        leftText.setTextColor(color);
        rightText.setTextColor(color);
    }

    public void setTitle(@StringRes int resId) {
        titleText.setText(resId);
    }

    public void setTitle(CharSequence title) {
        titleText.setText(title);
    }

    @Override
    public void setOnClickListener(@Nullable OnClickListener l) {
        leftImage.setOnClickListener(l);
        rightImage.setOnClickListener(l);
        leftText.setOnClickListener(l);
        rightText.setOnClickListener(l);
    }

    public void setLeftOnClickListener(@Nullable OnClickListener l) {
        leftImage.setOnClickListener(l);
        leftText.setOnClickListener(l);
    }

    public void setRightOnClickListener(@Nullable OnClickListener l) {
        rightImage.setOnClickListener(l);
        rightText.setOnClickListener(l);
    }

    public void leftExit(final Activity activity) {
        leftImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.finish();
            }
        });
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(mHeight, specMode);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
